#include <bits/stdc++.h>

using namespace std;

struct ListNode {
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(nullptr) {}
};

ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
  // write ur code here
  ListNode *list = nullptr;
  ListNode **thisnode = &list;
  while (l1 && l2) {
    if (l1->val < l2->val) {
      *(thisnode) = new ListNode(l1->val);
      l1 = l1->next;
    }
    else{
      *(thisnode) = new ListNode(l2->val);
      l2 = l2->next;
    }
    thisnode = &((*thisnode)->next);
  }
  while (l1) {
    *(thisnode) = new ListNode(l1->val);
    l1 = l1->next;
    thisnode = &((*thisnode)->next);
  }
  while (l2) {
    *(thisnode) = new ListNode(l2->val);
    l2 = l2->next;
    thisnode = &((*thisnode)->next);
  }
  return list;
}

#include "tools.h"